Thread: [Help]Correct or Wrong?Loops

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    26

    [Help]Correct or Wrong?Loops

    I have asked to write a program using for loops to create a hollow box but my code is

    Code:
    edited to prevent any abuse
    Is this acceptable? I left a few days before I have to pass up my assignment. Anyone please help?
    Last edited by exjames1991; 10-20-2009 at 12:10 AM.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by exjames1991 View Post
    I have asked to write a program using for loops to create a hollow box but my code is
    how come they're no "for loops"

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    26
    I miss out the word "for" in the question, it this acceptable? Anyone can guide me to change it to for loops??

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Code:
    	int height; /* initialize height */
    	int width; /* define width */
    	int n; /* define n */
    
    	printf("Enter the height and width:\n"); /* prints "Enter the height and width:" */
    		scanf("%d", &n); /* scans an integer and prints the value as n */
    
    		height = n; /* assign height */
    	width = n; /* assign width */
    over here


    Code:
    	int height; /* initialize height */
    	int width; /* define width */
    	int n; /* define n */
            int x;
    	printf("Enter the height and width:\n"); /* prints "Enter the height and width:" */
    
    scanf("%d %d", &n,&x) // WHERE BOTH VALUES ARE SPLIT BY A SPACE
    		height = n; /* assign height */
    	width = x; /* assign width */
    try the change it will work properly

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    26
    I thought for loops have for statements in the code?? Can you assist me to change from while loops to for loops

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To use for loops to do this, you need two of them:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* function main begins program execution */
    int main ()
    
    {
      int height; /* initialize height */
      int width;  /* define width */
      int high;   /* define high */
      int wide;   /* define wide */
      int n;        /* define n */
    
      printf("\n\n Enter the height and width [3-40]: "); /* prints "Enter the height and width:" */
      scanf("%d", &n); /* scans the height and width of the box */
    
      height = width = n;
      printf("\n\n");
    
      if ((width < 3) || (width > 40)) 
      { 
    	printf("An error has occur.\n"); /* prints "An error has occur." */
    	exit (1); /* exit program if has error */
      }
    
    
      for(high = 0; high < height; high++)  {
        printf("\n");
        for(wide = 0; wide < width; wide++) { 
          if(high == 0 || high == height-1)
            printf("*");   
          else { 
            if(wide == 0 || wide == width - 1)
              printf("*");
            else
              printf(" ");
          }
        }
      }
    
    
       return 0; /* indicate program ended successfully */
    } /* end function main */
    That might help.
    Last edited by Adak; 10-19-2009 at 02:16 AM.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    26
    Code:
    edited to prevent any abuse
    Last edited by exjames1991; 10-20-2009 at 12:11 AM.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by Adak View Post
    For loops are not in Obelisk's post. I'm not sure what he intended.

    To use for loops to do this, you need two of them:

    Code:
    
    
    I see the some other problems. Wait a bit while I put it into the "toaster".
    I just corrected the orginal post.....actually...i didnt add the for loops for the problem

  9. #9
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by exjames1991 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* function main begins program execution */
    int main ()
    
    {
    	int height; /* initialize height */
    	int width; /* define width */
    	int n; /* define n */
    
    	printf("Enter the height and the width\n");
    	scanf("%d", &n);
    
    	height = n;
    	width = n;
    
    	for(height = 0; height <= 40; height++)
    	{
    		for(width = 0; width <=40; width++)
    		{
    			if ((n >= 3) && (n <= 40))
                             	printf("%s", n==1||n==width||height==1||height==width ? "*" : " "); 
    			else if ((width < 3) || (width > 40))
    				printf("An error has occured.\n");
    			exit (1);
    			else
    				printf("\n");
    		}
    	}
    	return 0;
    }
    Help me, it says my error has illegal use of else without matching if, I am newbie to for statement, anyone help me plz? I am desperate :|
    correct the statement to this.. place the braces
    Code:
    			else if ((width < 3) || (width > 40))
    			{
    	printf("An error has occured.\n");
    			exit (1);
    }

  10. #10
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by exjames1991 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* function main begins program execution */
    int main ()
    
    {
    	int height; /* initialize height */
    	int width; /* define width */
    	int n; /* define n */
    
    	printf("Enter the height and the width\n");
    	scanf("%d", &n);
    
    	height = n;
    	width = n;
    
    	for(height = 0; height <= 40; height++)
    	{
    		for(width = 0; width <=40; width++)
    		{
    			if ((n >= 3) && (n <= 40))
                             	printf("%s", n==1||n==width||height==1||height==width ? "*" : " "); 
    			else if ((width < 3) || (width > 40))
    				printf("An error has occured.\n");
    			exit (1);
    			else
    				printf("\n");
    		}
    	}
    	return 0;
    }
    Help me, it says my error has illegal use of else without matching if, I am newbie to for statement, anyone help me plz? I am desperate :|

    your still missing the main point..here check the scanf statement and printfstatement

    Code:
    	printf("Enter the height and the width\n");
    	scanf("%d", &n);
    
    	height = n;
    	width = n;
    chk der...for example if you type..." 30 21" ..look the scanf statement will read only 30

    and will put height and width as 30...
    in order to correct it... try


    Code:
    int x;
    	printf("Enter the height and the width\n");
    	scanf("%d %d", &x,&n);
    
    	height = n;
    	width = x;
    Last edited by Obelisk; 10-19-2009 at 01:53 AM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ExJames:
    ======

    See post #6 for the update

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    26
    @ Adak THX(edited)

    Means that I have to enter 1 time which is the value of n to determine both of its height and width. Eg, I enter 5, it prints both 5 height and width. But what Adak wrote, I have to enter 2 different numbers to get different height and width. Eg, I enter 5 6, it prints 5 height and 6 width. Unless I misinterpret the question wrongly. Else if Adak mind to change the code abit so that when I enter 1 value it equals to both height and width?
    Last edited by exjames1991; 10-19-2009 at 04:42 AM.

Popular pages Recent additions subscribe to a feed